﻿using Admin.DAL;
using Admin.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace Admin.Controllers
{
    public class ProductAdminController : Controller
    {

       
       
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Product(string id)
        {
            ProductDAL pro = new ProductDAL();
            List<ProductModel> list = pro.SANPHAM(id);


            return View(list);
        }
        //DELETE
        public ActionResult deleteProduct(string id)
        {
            ProductDAL dal = new ProductDAL();
            List<ProductModel> list = dal.deletePR(id);
            return View(list);
        }

        [HttpPost, ActionName("DeleteProduct")]
        [ValidateAntiForgeryToken]

        public ActionResult Delete(string id)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from SANPHAM where MASP='" + id + "'", conn);
            cmd.ExecuteNonQuery();
            return RedirectToAction("Product");
        }
        //INSERT
        [HttpGet]
        public ActionResult insertProduct(string id)
        {

            return View();
        }
        [HttpPost]
        public ActionResult insertProduct(ProductModel model)
        {
            try
            {
                if(ModelState.IsValid)
                {
                    var asd = new ProductModel();
                    List<ProductModel> list = new List<ProductModel>();
                    SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
                    conn.Open();
                    SqlCommand command = new SqlCommand("insert into SANPHAM (TENSP,GIA) Values(N'" + model.TENSP + "',N'" + model.GIA + "')", conn);
                    SqlDataReader dr = command.ExecuteReader();
                    return RedirectToAction("Product");
                    
                }
                return View(model);

            }
            catch
            {
                return View();
            }
     
        }
        //EDIT
        [HttpGet]
        public ActionResult editProduct(string id)
        {
            
            ProductDAL dal = new ProductDAL();
            List<ProductModel> list = dal.editPR(id);
            return View(list);
           
           
        }


        public ActionResult Edit(string id, ProductModel model,FormCollection collection)
            {
            SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("Update SANPHAM set TENSP=N'"+model.TENSP+"', GIA=N'"+model.GIA+"' where MASP='" + id + "'", conn);
            cmd.ExecuteReader();
            if (collection["tensp"] != "")
            {
                model.TENSP = collection["tensp"];
            }
            else
            {
                return View("insertProduct");
            }
            if (collection["gia"] != "")
            {
                model.GIA = collection["gia"];
            }
            
            else
            {
                return View("insertProduct");
            }
            
            return RedirectToAction("Product");
        }
       
    }
}